package com.yz.learn.service.impl;

import com.yz.learn.entity.Item;
import com.yz.learn.service.StorageService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    ResourceLoader resourceLoader;
    @Override
    public List<Item> getAllStorage() throws IOException, InvalidFormatException {
        return StoragesDataByExcel();
    }

    private List<Item> StoragesDataByExcel() throws IOException, InvalidFormatException {
        File file = new ClassPathResource("templateFile/test.xlsx").getFile();
        InputStream is = new FileInputStream(file);
//        File file = new File("classpath:test.xlsx");
//        InputStream is = new FileInputStream(file.getAbsolutePath());
        // jxl提供的Workbook类
        Workbook wb = WorkbookFactory.create(is);
        // Excel的页签数量
        int sheet_size = wb.getNumberOfSheets();
        List<Item> outerList = new ArrayList<>();
        for (int index = 0; index < sheet_size; index++) {
            // 每个页签创建一个Sheet对象
            Sheet sheet = wb.getSheetAt(index);
            // sheet.getRows()返回该页的总行数
            System.out.println(sheet.getLastRowNum());
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                List innerList = new ArrayList();
                Row row = sheet.getRow(i);
                // sheet.getColumns()返回该页的总列数
                String cellinfo = "";
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    switch (cell.getCellType()){
                        case STRING:
                            cellinfo = cellinfo+cell.getStringCellValue() + " @";
                            break;
                        case NUMERIC:
                            cellinfo = cellinfo+cell.getNumericCellValue() + " @";
                            break;
                    }

                }
                String[] strings = cellinfo.split("@");
                Item item = new Item();
                item.setItemModel(strings[0]);
                item.setItemNum(strings[1]);
                item.setSnp(strings[2]);
                item.setBoxNum(Integer.valueOf(strings[3].substring(0,strings[3].indexOf("."))));
                item.setInStorageNum(Integer.valueOf(strings[4].substring(0,strings[4].indexOf("."))));
                item.setSafeInStorageNUm(Integer.valueOf(strings[5].substring(0,strings[5].indexOf("."))));
                item.setInStorageState(Integer.valueOf(strings[6].substring(0,strings[6].indexOf("."))));
                item.setKeepArea(strings[7]);
                item.setProductionLine(strings[8]);
                outerList.add(item);
            }

        }
        return outerList;
    }
}
